
exports.confirm_order = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};
    var type = 0;
    var cashCoupon = 0;
    var courseCoupon = 0;
    //任务流创建
    async.waterfall([
            //1.订单信息查询
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/o_id/"+ restfulParams.o_id,
                        method: 'POST',
                        data: {
                            fields: ["o_oid", "o_uid", "o_no", "o_name", "o_phone", "o_age", "o_type","o_money","o_pay_money","o_preferential_money","o_activity_type","o_coupon_id","o_aoc_id","o_preferential_money","o_sex","o_address","o_note","o_balance_pay","o_wechat_pay","o_organization_name","o_custom","o_pay_type","o_custom"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.orderData = data.data;
                        cb();
                    }
                );

            },
        function (cb) {
            //首先判断订单是啥订单;
            if(resultData.orderData[0].o_type == 2){
                //课程订单，搜索课程信息
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_course + "/oc_id/" + resultData.orderData[0].o_aoc_id,
                        method: 'POST',
                        data: {
                            fields: ["oc_title", "oc_title_image", "oc_old_price", "oc_new_price", "oc_course_count", "oc_type"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        type = 1;
                        resultData.courseData = data.data;

                        cb();
                    }
                );

            }
            if(resultData.orderData[0].o_type == 0){
                //机构活动
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_organization_activity + "/oa_id/" + resultData.orderData[0].o_aoc_id,
                        method: 'POST',
                        data: {
                            fields: ["oa_title", "oa_title_image","oa_end_time"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        type = 2;
                        resultData.organizationActivityData = data.data;
                        cb();
                    }
                );
            }
            if(resultData.orderData[0].o_type == 1){
                //论坛活动
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_forum_activity + "/fa_id/" + resultData.orderData[0].o_aoc_id,
                        method: 'POST',
                        data: {
                            fields: ["fa_title", "fa_image","fa_price"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.forumActivityData = data.data;
                        cb();
                    }
                );
            }
        },
        /********************************************************************/
        function (cb) {
            //判断是否是机构课程和机构活动
            if(type == 1 || type==2){
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchCashCoupon + "/ucc_id/" + resultData.orderData[0].o_coupon_id,
                            method: 'POST',
                            data: {
                                fields: ["ucc_ccid","ucc_from_type"].join(",")
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }

                            cashCoupon = data.data;
                            cb();
                        }
                    );
                } else{
                cb();
            }
        },
        function (cb) {
            ////console.log(cashCoupon);
        if(cashCoupon.length>0){
            if (type == 1 || type == 2) {
                if (cashCoupon[0].ucc_from_type == 1) {//平台发放的，去平台表里查询代金券信息
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_platform_coupon + "/pc_id/" + cashCoupon[0].ucc_ccid,
                            method: 'POST',
                            data: {
                                fields: ["pc_price as price", "pc_name as name", "pc_start_time as start_time", "pc_end_time as end_time"].join(",")
                            }
                        },
                        function (err, data) {
                            ////console.log("-----------------");
                            // ///console.log(data.data);
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }

                            resultData.cashCoupondata = data.data[0];
                            cb();
                        }
                    );

                } else if (cashCoupon[0].ucc_from_type == 0) {//机构发放的
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_cash_coupon + "/cc_id/" + cashCoupon[0].ucc_ccid,
                            method: 'POST',
                            data: {
                                fields: ["cc_price as price", "cc_name as name", "cc_start_time as start_time", "cc_end_time as end_time"].join(",")
                            }
                        },
                        function (err, data) {
                            ////console.log("-----------------");
                            ////console.log(data.data);
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }

                            resultData.cashCoupondata = data.data[0];
                            cb();
                        }
                    );
                }
            }
        }else{
            cb();
        }
        }

        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );
}